<template>
  <!--   期权条款     -->
  <div id='instruments' style="display: inline-flex">
    <div class="instrumentList" v-for="(v,i) in instruments" v-bind:key="i"
         style="display: inline-flex;margin: 3px 0px 0px 0px;">
      <component :is="getInstrument(instruments[i].optionType)"
                 :disabled=true
                 :del="props.close"
                 :useType="'show'"
                 :ref="'instrument'+instruments[i].index"
                 :formData="instruments[i].formData"
                 :index="instruments[i].index"
                 :cfgId="instruments[i].cfgId"
                 :calcPricingParam="instruments[i].calcPricingParam"
                 :valueDate="calcPricingParam.valueDate"
                 :cacheFormData="cacheFormData"
                 :result="instruments[i].result"/>
    </div>
  </div>
</template>

<script setup>
import {defineAsyncComponent, onMounted, reactive} from 'vue'
import {getInstrument} from "@/views/calc/otcOtion/instrument/common/common";
const {proxy} = getCurrentInstance()
function cacheFormData() {
  //暂时不处理
}

let instruments = reactive([])
let calcPricingParam = reactive({
  data: {}
})

const props = defineProps({
  close: {
    type: Function,
    default: null
  }
})


function initCompent() {
  if (proxy.$attrs.instrument) {
    instruments.length = 0
    var {
      pForm,
      pFormPricing,
      pFormInstrument,
      productType,
      optionType,
      pFormResult,
      P_RESPONSE
    } = proxy.$attrs.instrument
    calcPricingParam.data = JSON.parse(String(pFormPricing)) || {}
    // pFormResult = pFormResult || {}
    if (getInstrument(optionType)){
      var ele = {
        index: 0,
        optionType: String(optionType).toLowerCase(),
        calcPricingParam: calcPricingParam.data,
      }
      pFormResult = typeof pFormResult == 'object' ?pFormResult :JSON.parse(pFormResult)
      if (Boolean(proxy.$attrs.instrument.pFormResult)) {
        ele.formData = proxy.lsh.assign({data: JSON.parse(pFormInstrument)},{result:pFormResult })
      } else {
        ele.formData = {data: JSON.parse(pFormInstrument)}
      }
      ele.result = pFormResult

      instruments.push(ele)
    }
  }
}

onMounted(() => {
  initCompent()
})
</script>

